import requests
from django.utils.deprecation import MiddlewareMixin
from django.shortcuts import render, HttpResponse

from Tool_module.tool_api import Myjwt_token
from djangoProject import settings


class MyMiddlewareMixin(MiddlewareMixin):
    def process_request(self, request):
        # print("md1  process_request 方法。", request.META.get("HTTP_X_HIOSHOP_TOKEN"))  # 在视图之前执行
        # print(request.META,'======================request.data')
        token = request.META.get("HTTP_X_HIOSHOP_TOKEN")
        user_id = Myjwt_token(token)
        user_id = user_id.jiemi().get('user_id')
        request.META['user_id']=user_id
        # code = request.data.get('code')
        # code_url = settings.code2Session.format(settings.AppId, settings.AppSecret, code)
        # response = requests.get(code_url)
        # json_response = response.json()  # 把它变成json的字典
        print(request.META, "==========================================中间件======")

    def process_response(self, request, response):
        # print("md1  process_response 方法！", request.META.get("HTTP_X_HIOSHOP_TOKEN"))
        print(request.META.get('user_id'),'=======================------------------返回请求')
        return response
